
from collections import  OrderedDict
from     pathlib import  Path
import  queue

class Misregistraion:
    def __init__(self,size = 8):
        # self.q = queue.Queue(maxsize=queue_size)
        self.size = size
        self.rows = OrderedDict()   #   行号-名称
        self.number = {}
        #   额外一个字典存放 行号-计数
        #   需要有序，需要行号进行索引，需要去重，相同错误计数增加
        self.more = OrderedDict()
    def __len__(self):
        return  len(self.rows)
    def __iter__(self):
        yield from self.rows.items()
    def all(self):  #   展示全部数据
        self.show()
        if self.more:
            print('以下为更多数据')
            for k,v in sorted(self.more.items(),reverse=True):
                print(v,':',k)
    def show(self): #   展示至多八条最新数据
        if not self.rows:
            print( "当前记录为空")
        else:
            for line,name in self.rows.items():
                print(name,':',line)
    def _add(self,src:str): #   加入记录
        src = src.split()
        name = self._max_char(Path(src[0]).name)
        line = src[-1]
        if line in (self.rows or  self.more):
            if name == self.rows[line] or self.more[line]:
                self.number[line+' '+name]=self.number.get(line+' '+name,1)+1
                return self
        if len(self.rows)<8:
            self.rows[line] = name
        else:
            self._move()
            self.rows[line] = name
        return  self
    def _move(self):   #先进先出，进入more序列
        line = self._ret_first() #取出了有序字典的首位元素key
        self.more[line] = self.rows[line]
        self.rows.pop(line)
    def _ret_first(self):   #   提取有序字典首位元素的索引key
        for i in self.rows.keys():
            return i
    def add(self,srcs): #   识别多条记录在一个字符串内，或多条记录组为一个迭代序列,这个序列最好有序
        if isinstance(srcs,str):
            srcs= srcs.split('\n')
        elif isinstance(srcs,(list,tuple)):
            pass
        else:
            return "请输入有序迭代序列，最新数据位于最后"
        for src in srcs:
            if src:
                self._add(src)
    def _max_char(self,name:str):
        if len(name)>16:
            return name[-16:]
        return  name


a = Misregistraion()
a.add(r'E:\V1R2\product\fpgsdaaxca.c 1255')
# a.show()
# print(len(a))
# print(list(a))
# a.show()
a.all()
# 写的不错~








